package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.AddressDto;
import io.geekidea.boot.shop.entity.Address;
import io.geekidea.boot.shop.mapper.AddressMapper;
import io.geekidea.boot.shop.query.app.AppAddressQuery;
import io.geekidea.boot.shop.query.web.AddressQuery;
import io.geekidea.boot.shop.service.base.api.AddressService;
import io.geekidea.boot.shop.vo.app.AppAddressVo;
import io.geekidea.boot.shop.vo.web.AddressVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 收货地址 服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addAddress(AddressDto dto) {
        Address address = new Address();
        BeanUtils.copyProperties(dto, address);
        return save(address);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateAddress(AddressDto dto) {
        Integer id = dto.getId();
        Address address = getById(id);
        if (address == null) {
            throw new BusinessException("收货地址不存在");
        }
        BeanUtils.copyProperties(dto, address);
        return updateById(address);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteAddress(Integer id) {
        return removeById(id);
    }

    @Override
    public AddressVo getAddressById(Integer id) {
        return addressMapper.getAddressById(id);
    }

    @Override
    public Paging<AddressVo> getAddressPage(AddressQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AddressVo> list = addressMapper.getAddressPage(query);
        Paging<AddressVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppAddressVo getAppAddressById(Integer id) {
        return addressMapper.getAppAddressById(id);
    }

    @Override
    public Paging<AppAddressVo> getAppAddressPage(AppAddressQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppAddressVo> list = addressMapper.getAppAddressPage(query);
        Paging<AppAddressVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public Address getByMemberId(Long id) {
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getMemberId, id);
        wrapper.eq(Address::getIsDefault, 1);
        return getOne(wrapper);
    }
}
